using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

[CustomEditor(typeof(HMI_Button), true)]
public class HMI_Button_Editor : HMI_EditorBase
{
    [SerializeField]
    private HMI_Button mTarget;
    private MyText mText;
    private LPImage mIcon;

    private Sprite curIcon;
    private bool hasFrameAni; //帧动画

    public override void Awake()
    {
        base.Awake();
        mTarget = target as HMI_Button;

        mText = mTarget.transform.Find("Button/Text")?.GetComponent<MyText>();
        mIcon = mTarget.transform.Find("Button/Icon")?.GetComponent<LPImage>();

        if (isIcon())
        {
            curIcon = mIcon.sprite;
            hasFrameAni = mIcon.GetComponent<SingleFrameAni>() != null;
        } 
        else
            hasFrameAni = false;

        InitDecoration();
    }

    public override void OnInspectorGUI()
    {
        EditorGUI.BeginChangeCheck();
        if (isText())
        {
            mText.text = EditorGUILayout.TextField("文字：", mText.text);
        }

        if (isIcon())
        {
            curIcon = (Sprite)EditorGUILayout.ObjectField("图片：", curIcon, typeof(Sprite), false);
            var targetSp = mIcon.gameObject.GetComponent<Image>().sprite;
            if (HMI_Slider_Editor.isFrameAniName(targetSp))
                hasFrameAni = EditorGUILayout.Toggle("使用帧动画：", hasFrameAni);
        }
        if (EditorGUI.EndChangeCheck())
        {
            if (isText())
                mTarget.SetVoiceText(mText.text);
            if (isIcon())
            {
                mIcon.sprite = curIcon;
                mIcon.SetNativeSize();
                RefreshFrameAni();
            }
            EditorUtility.SetDirty(mTarget);
        }

        base.OnInspectorGUI();
        DrawVoice();
        DrawDecoration();
        DrawStyle();
    }

    private void RefreshFrameAni()
    {
        var iconCom = mIcon.GetComponent<SingleFrameAni>();
        if (hasFrameAni)
        {
            if (iconCom == null)
            {
                iconCom = mIcon.gameObject.AddComponent<SingleFrameAni>();
            }
            iconCom.SetSprite();
        }
        else
        {
            if (iconCom != null)
            {
                DestroyImmediate(iconCom);
            }
        }
    }

    #region 类型判断
    private bool isText()
    {
        return mTarget.widgetType != WidgetType_Button_Image;
    }
    private bool isIcon()
    {
        return mTarget.widgetType == WidgetType_Button_Image || mTarget.widgetType == WidgetType_IconHorizontalButton || mTarget.widgetType == WidgetType_IconVerticalButton;
    }
    private bool isPureText()
    {
        return mTarget.widgetType == WidgetType_Button_Text;
    }
    private bool isPureIcon()
    {
        return mTarget.widgetType == WidgetType_Button_Image;
    }
    #endregion 类型判断

    #region 可见即可说
    private bool voiceFoldout = false;
    private void DrawVoice()
    {
        if (!isText())
        {
            voiceFoldout = EditorToolsUtils.CreateFoldout("语音", DrawVoiceFoldout, voiceFoldout);
        }
    }

    private void DrawVoiceFoldout()
    {
        EditorGUI.BeginChangeCheck();
        mTarget.VoiceText = EditorGUILayout.TextField("语音文本：", mTarget.VoiceText);
        if (EditorGUI.EndChangeCheck())
        {
            mTarget.SetVoiceText(mTarget.VoiceText);
            EditorUtility.SetDirty(mTarget);
        }
    }
    #endregion 可见即可说

    #region 装饰
    private bool decorationFoldout = false;
    private bool hasArrow; //角标图标
    private GameObject arrowObj;

    private bool hasRightArrow; //右箭头图标
    private GameObject rightArrowObj;
    private void InitDecoration()
    {
        arrowObj = mTarget.transform.Find("Arrow")?.gameObject;
        rightArrowObj = mTarget.transform.Find("RightArrow")?.gameObject;

        hasArrow = (arrowObj != null);
        hasRightArrow = (rightArrowObj != null);
    }

    private void DrawDecoration()
    {
        if (!isPureText() && !isPureIcon())
        {
            decorationFoldout = EditorToolsUtils.CreateFoldout("装饰", DrawDecorationFoldout, decorationFoldout);
        }
    }

    private void DrawDecorationFoldout()
    {
        EditorGUI.BeginChangeCheck();
        hasArrow = EditorGUILayout.Toggle("有角标：", hasArrow);
        hasRightArrow = EditorGUILayout.Toggle("有右箭头：", hasRightArrow);
        if (EditorGUI.EndChangeCheck())
        {
            RefreshObj();
            EditorUtility.SetDirty(mTarget);
        }
    }

    private void RefreshObj()
    {
        if (hasArrow)
        {
            if (arrowObj == null)
            {
                arrowObj = HMI_Slider_Editor.LoadMiniWidget(mTarget.transform, "Arrow");
            }
        }
        else
        {
            var arrowGo = mTarget.transform.Find("Arrow");
            if (arrowGo != null)
            {
                arrowObj = null;
                DestroyImmediate(arrowGo.gameObject);
            }

        }

        if (hasRightArrow)
        {
            if (rightArrowObj == null)
            {
                rightArrowObj = HMI_Slider_Editor.LoadMiniWidget(mTarget.transform, "RightArrow");
            }
        }
        else
        {
            var rightArrowGo = mTarget.transform.Find("RightArrow");
            if (rightArrowGo != null)
            {
                rightArrowObj = null;
                DestroyImmediate(rightArrowGo.gameObject);
            }
        }
    }
    #endregion 装饰

    #region 样式
    private bool styleFoldout = false;
    private bool foldoutText = false;
    private MyTextEditor textEditor;
    private bool foldoutBtn = false;
    private LPImage mImgBtn;
    private LPImageEditor btnEditor;
    private bool foldoutIcon = false;
    private LPImageEditor imageEditor;
    private void DrawStyle()
    {
        styleFoldout = EditorToolsUtils.CreateFoldout("样式", DrawStyleFoldout, styleFoldout);
    }

    private void DrawStyleFoldout()
    {
        EditorGUI.BeginChangeCheck();
        EditorGUILayout.Separator();
        if (isText())
        {
            GUILayout.Label("文字：");
            if (textEditor == null)
                textEditor = CreateEditor(mText) as MyTextEditor;
            foldoutText = textEditor.CreateStyleEditor(foldoutText);
            EditorGUILayout.Separator();
        }

        if (!isText())
        {
            GUILayout.Label("按钮背景图：");
            if (mImgBtn == null)
                mImgBtn = mTarget.transform.Find("Button")?.GetComponent<LPImage>();
            if (btnEditor == null)
                btnEditor = CreateEditor(mImgBtn) as LPImageEditor;
            foldoutBtn = btnEditor.CreateStyleEditor(foldoutBtn);
            EditorGUILayout.Separator();
        }

        if (isIcon())
        {
            GUILayout.Label("图标：");
            if (imageEditor == null)
                imageEditor = CreateEditor(mIcon) as LPImageEditor;
            foldoutIcon = imageEditor.CreateStyleEditor(foldoutIcon);
            EditorGUILayout.Separator();
        }
        if (EditorGUI.EndChangeCheck())
        {
            EditorUtility.SetDirty(mTarget);
        }
    }
    #endregion 样式

    #region 序列化
    protected override string GetDataJson()
    {
        var data = new HMI_ButtonData();

        if (isText())
            data.TextData = mText.GetData();

        if (isIcon())
            data.IconData = mIcon.GetData(AssetDatabase.GetAssetPath(curIcon));

        data.HasArrow = hasArrow;
        data.HasRightArrow = hasRightArrow;
        data.HasFrameAni = hasFrameAni;

        string json = JsonUtility.ToJson(data, true);
        return json;
    }

    protected override void SetData(string json)
    {
        HMI_ButtonData data = JsonUtility.FromJson<HMI_ButtonData>(json);

        if (isText() && mText != null && data.TextData != null)
        {
            MyTextData textData = data.TextData;
            mText.SetData(textData);
        }
        if (isIcon() && mIcon != null && data.IconData != null)
        {
            LPImageData imageData = data.IconData;
            mIcon.SetData(imageData);

            hasFrameAni = data.HasFrameAni;
            if (hasFrameAni)
                RefreshFrameAni();
        }

        hasArrow = data.HasArrow;
        hasRightArrow = data.HasRightArrow;

        RefreshObj();

        base.SetData(json);
        EditorUtility.SetDirty(mTarget);
    }
    #endregion 序列化
}

[Serializable]
public class HMI_ButtonData
{
    public MyTextData TextData;  //文字
    public LPImageData IconData;  //图片
    public bool HasArrow;  //是否有箭头
    public bool HasRightArrow;  //是否有右箭头
    public bool HasFrameAni;  //是否有帧动画
}
